﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Collections;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;
    public class PresentAwards
    {
        public int Add(Chain.Model.PresentAwards model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into PresentAwards(");
            builder.Append("PresentsName,Validity,Status,isdel)");
            builder.Append(" values (");
            builder.Append("@PresentsName,@Validity,@Status,@isdel)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@PresentsName", SqlDbType.VarChar, 50),
                new SqlParameter("@Validity", SqlDbType.Int, 4),
                new SqlParameter("@Status", SqlDbType.Int, 4),
                new SqlParameter("@isdel", SqlDbType.Int, 4)
             };
            cmdParms[0].Value = model.PresentsName;
            cmdParms[1].Value = model.Validity;
            cmdParms[2].Value = model.Status;
            cmdParms[3].Value = model.isdel;

            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }
        public bool Update(Chain.Model.PresentAwards model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update PresentAwards set ");
            builder.Append("PresentsName=@PresentsName,");
            builder.Append("Validity=@Validity,");
            builder.Append("Status=@Status,");
            builder.Append("isdel=@isdel");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@PresentsName", SqlDbType.VarChar, 50),
                new SqlParameter("@Validity", SqlDbType.Int, 4),
                new SqlParameter("@Status", SqlDbType.Int, 4),
                new SqlParameter("@isdel", SqlDbType.Int, 4),
                new SqlParameter("@ID", SqlDbType.Int, 4)
             };
            cmdParms[0].Value = model.PresentsName;
            cmdParms[1].Value = model.Validity;
            cmdParms[2].Value = model.Status;
            cmdParms[3].Value = model.isdel;
            cmdParms[4].Value = model.ID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
        public bool IsDelete(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update PresentAwards set isdel=1");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
        public DataSet GetItem(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from PresentAwards ");
            builder.Append(" where ID=@ID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            return DbHelperSQL.Query(builder.ToString(), cmdParms);
        }
        public Chain.Model.PresentAwards GetModel(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select top 1 *");
            builder.Append(" from PresentAwards");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                return this.DataRowToModel(set.Tables[0].Rows[0]);
            }
            return null;
        }
        public Chain.Model.PresentAwards DataRowToModel(DataRow row)
        {
            Chain.Model.PresentAwards model = new Chain.Model.PresentAwards();
            if (row != null)
            {
                if ((row["ID"] != null) && (row["ID"].ToString() != ""))
                {
                    model.ID = int.Parse(row["ID"].ToString());
                }
                if ((row["PresentsName"] != null) && (row["PresentsName"].ToString() != ""))
                {
                    model.PresentsName = row["PresentsName"].ToString();
                }
                if ((row["Validity"] != null) && (row["Validity"].ToString() != ""))
                {
                    model.Validity = int.Parse(row["Validity"].ToString());
                }
                if ((row["Status"] != null) && (row["Status"].ToString() != ""))
                {
                    model.Status = int.Parse(row["Status"].ToString());
                }
                if (row["isdel"] != null && (row["isdel"].ToString() != ""))
                {
                    model.isdel = int.Parse(row["isdel"].ToString());
                }
            }
            return model;
        }
    }
}